package com.cnbot.idcardtest;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.cnbot.dcidcard.DcCardApi;
import com.cnbot.dcidcard.IdCardInfoBean;

public class MainActivity extends AppCompatActivity {
    private final String TAG = MainActivity.class.getSimpleName();

    private DcCardApi dcCardApi = null;
    private ImageView imgPhoto = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgPhoto = (ImageView) findViewById(R.id.img_photo);

        dcCardApi = new DcCardApi(MainActivity.this);
        int state = dcCardApi.openUsbDevice();
        Log.e(TAG, "打开状态 = " + state);
    }

    public void onIdCardClick(View view) {
        IdCardInfoBean idCard = dcCardApi.readSingleCardInfo();
        if(idCard!= null) {
            Log.e(TAG, "anme = " + idCard.getName());
            Log.e(TAG, "sex = " + idCard.getSex());
            Log.e(TAG, "nation = " + idCard.getNation());
            Log.e(TAG, "birthday = " + idCard.getBirthday());

            Log.e(TAG, "address = " + idCard.getAddress());
            Log.e(TAG, "id = " + idCard.getId());
            Log.e(TAG, "startTime = " + idCard.getStartTime());
            Log.e(TAG, "endTime = " + idCard.getEndTime());
            imgPhoto.setImageBitmap(BitmapFactory.decodeByteArray(idCard.getPhotoData(), 0, idCard.getPhotoData().length));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        int state = dcCardApi.closeUsbDevice();
        Log.e(TAG, "关闭状态 = " + state);
    }
}
